﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;
using ThreadManager.Restrictions;
namespace OurCrawler
{
    class ThreadManager
    {

        public int threadCount;
        Thread[] threads;
        Thread threadParse;
        public List<Thread> lThreads;
        bool canAdd; 
        bool fix;
        private Restrictions restrictions ;
       
        //MyQueue myQueue;

        public ThreadManager()
        {
            threadCount = 0;
            restrictions = new Restrictions();
            canAdd = false ;
            fix = false;
            lThreads = new List<Thread>();

        }
        public ThreadManager( int _threadCount )
        {
            threadCount = _threadCount;
            restrictions = new Restrictions();
            canAdd = false;
            fix = true;
            lThreads = new List<Thread>( threadCount );
        }
        public bool CanAdd
        {
            get
            {
                return canAdd;
            }
        }
        private void ThreadRun()
        {
            
        }

        public void verifyRestrictions_adaptive()
        {
           
                Thread verifyProcessorUsage_th = new Thread(new ThreadStart(verifyProcessorUsage));
                verifyProcessorUsage_th.Start();

                Thread verifyInternetConnection_th = new Thread(new ThreadStart(verifyInternetConnection));
                verifyInternetConnection_th.Start();
            
        }


        public void verifyRestrictions_fixed()
        {

                Thread verifyInternetConnection_th = new Thread(new ThreadStart(verifyInternetConnection));
                verifyInternetConnection_th.Start();
            
        }
       
        public void Start( bool fix )
        {


            if ( !fix )
            {
                Thread.Sleep(5000);
                Thread verifyRestrictions_adaptive_th = new Thread( new ThreadStart( verifyRestrictions_adaptive ) );
                verifyRestrictions_adaptive_th.Start();

            }
            else
            {
                Thread.Sleep(5000);
                Thread verifyRestrictions_fixed_th = new Thread( new ThreadStart( verifyRestrictions_fixed ) );
                verifyRestrictions_fixed_th.Start();

                
            }
        }

        private void verifyInternetConnection()
        {
            while (true)
            {
                if (!restrictions.ConnectionExists())
                {
                    canAdd = false;
                }
                else
                {
                    canAdd = true;
                }
            }
        }

        private void verifyProcessorUsage()
        {
            while (true)
            {
                if (restrictions.ProcessorUsage < 75 ) canAdd = true;
                else canAdd = false;
            }
        }


        //private void CreateThreads()
        //{
        //    threads = new Thread[threadCount];
        //    //string[] subItems = { "", "" };

        //    //for (int j = 0; j < threadCount; j++)
        //    //{
        //    //    list_lview.Items.Add(j.ToString());
        //    //    list_lview.Items[j].SubItems.AddRange(subItems);
        //    //}
        //    //Monitor.Enter(list_lview);
        //    //for (int i = 0; i < threadCount; i++)
        //    //{

        //    //    //list_lview.Items[i].SubItems[1].Text = i.ToString();
        //    //    threads[i] = new Thread(new ThreadStart(ThreadRun));
        //    //    threads[i].Name = i.ToString();
        //    //    threads[i].Start();

        //    //}
        //    //Monitor.Exit(list_lview);


        //}

        

    }
}
